home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spambayes / hammie.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  9.8 KB  |  282 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from spambayes import mboxutils
  5. from spambayes import storage
  6. from spambayes.Options import options
  7. from spambayes.tokenizer import tokenize
  8.  
  9. try:
  10.     (True, False)
  11. except NameError:
  12.     (True, False) = (1, 0)
  13.  
  14.  
  15. class Hammie:
  16.     '''A spambayes mail filter.
  17.  
  18.     This implements the basic functionality needed to score, filter, or
  19.     train.
  20.  
  21.     '''
  22.     
  23.     def __init__(self, bayes):
  24.         self.bayes = bayes
  25.  
  26.     
  27.     def _scoremsg(self, msg, evidence = False):
  28.         '''Score a Message.
  29.  
  30.         msg can be a string, a file object, or a Message object.
  31.  
  32.         Returns the probability the message is spam.  If evidence is
  33.         true, returns a tuple: (probability, clues), where clues is a
  34.         list of the words which contributed to the score.
  35.  
  36.         '''
  37.         return self.bayes.spamprob(tokenize(msg), evidence)
  38.  
  39.     
  40.     def formatclues(self, clues, sep = '; '):
  41.         '''Format the clues into something readable.'''
  42.         return [](_[1])
  43.  
  44.     
  45.     def score(self, msg, evidence = False):
  46.         '''Score (judge) a message.
  47.  
  48.         msg can be a string, a file object, or a Message object.
  49.  
  50.         Returns the probability the message is spam.  If evidence is
  51.         true, returns a tuple: (probability, clues), where clues is a
  52.         list of the words which contributed to the score.
  53.  
  54.         '''
  55.         return self._scoremsg(msg, evidence)
  56.  
  57.     
  58.     def score_and_filter(self, msg, header = None, spam_cutoff = None, ham_cutoff = None, debugheader = None, debug = None, train = None):
  59.         """Score (judge) a message and add a disposition header.
  60.  
  61.         msg can be a string, a file object, or a Message object.
  62.  
  63.         Optionally, set header to the name of the header to add, and/or
  64.         spam_cutoff/ham_cutoff to the probability values which must be met
  65.         or exceeded for a message to get a 'Spam' or 'Ham' classification.
  66.  
  67.         An extra debugging header can be added if 'debug' is set to True.
  68.         The name of the debugging header is given as 'debugheader'.
  69.  
  70.         If 'train' is True, also train on the result of scoring the
  71.         message (ie. train as ham if it's ham, train as spam if it's
  72.         spam).  If the message already has a trained header, it will be
  73.         untrained first.  You'll want to be very dilligent about
  74.         retraining mistakes if you use this option.
  75.  
  76.         All defaults for optional parameters come from the Options file.
  77.  
  78.         Returns the score and same message with a new disposition header.
  79.         """
  80.         if header == None:
  81.             header = options[('Headers', 'classification_header_name')]
  82.         
  83.         if spam_cutoff == None:
  84.             spam_cutoff = options[('Categorization', 'spam_cutoff')]
  85.         
  86.         if ham_cutoff == None:
  87.             ham_cutoff = options[('Categorization', 'ham_cutoff')]
  88.         
  89.         if debugheader == None:
  90.             debugheader = options[('Headers', 'evidence_header_name')]
  91.         
  92.         if debug == None:
  93.             debug = options[('Headers', 'include_evidence')]
  94.         
  95.         if train == None:
  96.             train = options[('Hammie', 'train_on_filter')]
  97.         
  98.         msg = mboxutils.get_message(msg)
  99.         
  100.         try:
  101.             del msg[header]
  102.         except KeyError:
  103.             pass
  104.  
  105.         if train:
  106.             self.untrain_from_header(msg)
  107.         
  108.         (prob, clues) = self._scoremsg(msg, True)
  109.         if prob < ham_cutoff:
  110.             is_spam = False
  111.             disp = options[('Headers', 'header_ham_string')]
  112.         elif prob > spam_cutoff:
  113.             is_spam = True
  114.             disp = options[('Headers', 'header_spam_string')]
  115.         else:
  116.             is_spam = False
  117.             disp = options[('Headers', 'header_unsure_string')]
  118.         if train:
  119.             self.train(msg, is_spam, True)
  120.         
  121.         basic_disp = disp
  122.         disp += '; %.*f' % (options[('Headers', 'header_score_digits')], prob)
  123.         if options[('Headers', 'header_score_logarithm')]:
  124.             if prob <= 0.0050000000000000001 and prob > 0.0:
  125.                 import math
  126.                 x = -math.log10(prob)
  127.                 disp += ' (%d)' % x
  128.             
  129.             if prob >= 0.995 and prob < 1.0:
  130.                 import math
  131.                 x = -math.log10(1.0 - prob)
  132.                 disp += ' (%d)' % x
  133.             
  134.         
  135.         del msg[header]
  136.         msg.add_header(header, disp)
  137.         for header in ('to', 'subject'):
  138.             if basic_disp in options[('Headers', 'notate_' + header)]:
  139.                 orig = msg[header]
  140.                 del msg[header]
  141.                 msg[header] = '%s,%s' % (basic_disp, orig)
  142.                 continue
  143.         
  144.         if debug:
  145.             disp = self.formatclues(clues)
  146.             del msg[debugheader]
  147.             msg.add_header(debugheader, disp)
  148.         
  149.         result = mboxutils.as_string(msg, unixfrom = msg.get_unixfrom() is not None)
  150.         return (prob, result)
  151.  
  152.     
  153.     def filter(self, msg, header = None, spam_cutoff = None, ham_cutoff = None, debugheader = None, debug = None, train = None):
  154.         (prob, result) = self.score_and_filter(msg, header, spam_cutoff, ham_cutoff, debugheader, debug, train)
  155.         return result
  156.  
  157.     
  158.     def train(self, msg, is_spam, add_header = False):
  159.         '''Train bayes with a message.
  160.  
  161.         msg can be a string, a file object, or a Message object.
  162.  
  163.         is_spam should be 1 if the message is spam, 0 if not.
  164.  
  165.         If add_header is True, add a header with how it was trained (in
  166.         case we need to untrain later)
  167.  
  168.         '''
  169.         self.bayes.learn(tokenize(msg), is_spam)
  170.         if add_header:
  171.             if is_spam:
  172.                 trained = options[('Headers', 'header_spam_string')]
  173.             else:
  174.                 trained = options[('Headers', 'header_ham_string')]
  175.             del msg[options[('Headers', 'trained_header_name')]]
  176.             msg.add_header(options[('Headers', 'trained_header_name')], trained)
  177.         
  178.  
  179.     
  180.     def untrain(self, msg, is_spam):
  181.         '''Untrain bayes with a message.
  182.  
  183.         msg can be a string, a file object, or a Message object.
  184.  
  185.         is_spam should be True if the message is spam, False if not.
  186.  
  187.         '''
  188.         self.bayes.unlearn(tokenize(msg), is_spam)
  189.  
  190.     
  191.     def untrain_from_header(self, msg):
  192.         '''Untrain bayes based on X-Spambayes-Trained header.
  193.  
  194.         msg can be a string, a file object, or a Message object.
  195.  
  196.         If no such header is present, nothing happens.
  197.  
  198.         If add_header is True, add a header with how it was trained (in
  199.         case we need to untrain later)
  200.  
  201.         '''
  202.         msg = mboxutils.get_message(msg)
  203.         trained = msg.get(options[('Headers', 'trained_header_name')])
  204.         if not trained:
  205.             return None
  206.         
  207.         del msg[options[('Headers', 'trained_header_name')]]
  208.         if trained == options[('Headers', 'header_ham_string')]:
  209.             self.untrain_ham(msg)
  210.         elif trained == options[('Headers', 'header_spam_string')]:
  211.             self.untrain_spam(msg)
  212.         else:
  213.             raise ValueError('%s header value unrecognized' % options[('Headers', 'trained_header_name')])
  214.  
  215.     
  216.     def train_ham(self, msg, add_header = False):
  217.         '''Train bayes with ham.
  218.  
  219.         msg can be a string, a file object, or a Message object.
  220.  
  221.         If add_header is True, add a header with how it was trained (in
  222.         case we need to untrain later)
  223.  
  224.         '''
  225.         self.train(msg, False, add_header)
  226.  
  227.     
  228.     def train_spam(self, msg, add_header = False):
  229.         '''Train bayes with spam.
  230.  
  231.         msg can be a string, a file object, or a Message object.
  232.  
  233.         If add_header is True, add a header with how it was trained (in
  234.         case we need to untrain later)
  235.  
  236.         '''
  237.         self.train(msg, True, add_header)
  238.  
  239.     
  240.     def untrain_ham(self, msg):
  241.         '''Untrain bayes with a message previously trained as ham.
  242.  
  243.         msg can be a string, a file object, or a Message object.
  244.  
  245.         '''
  246.         self.untrain(msg, False)
  247.  
  248.     
  249.     def untrain_spam(self, msg):
  250.         '''Untrain bayes with a message previously traned as spam.
  251.  
  252.         msg can be a string, a file object, or a Message object.
  253.  
  254.         '''
  255.         self.untrain(msg, True)
  256.  
  257.     
  258.     def store(self):
  259.         '''Write out the persistent store.
  260.  
  261.         This makes sure the persistent store reflects what is currently
  262.         in memory.  You would want to do this after a write and before
  263.         exiting.
  264.  
  265.         '''
  266.         self.bayes.store()
  267.  
  268.  
  269.  
  270. def open(filename, useDB = 'dbm', mode = 'r'):
  271.     """Open a file, returning a Hammie instance.
  272.  
  273.     mode is used as the flag to open DBDict objects.  'c' for read-write
  274.     (create if needed), 'r' for read-only, 'w' for read-write.
  275.     """
  276.     return Hammie(storage.open_storage(filename, useDB, mode))
  277.  
  278. if __name__ == '__main__':
  279.     import hammiebulk
  280.     hammiebulk.main()
  281.  
  282.